<?php
class controller_ajax extends model_ajax{
    public function __construct() {
        //默认不用登录
        $this->usercheck = false;
        parent::__construct();
    }
    /*
     * 生成图片缩例图
     */
    public function imgcorp(){
        $this->user_check_func();
        //定义允许剪裁的文件扩展名
        $ext_arr = array(
            'image' => array('gif', 'jpg', 'png', 'bmp'),
        );
        $imgfile = strval($_GET['imgfile']);
        if(empty($imgfile)) $imgfile = $imgfile['name'];
        $php_path = PATH_ROOT;
        $sourceFile = $php_path . $imgfile;
        //获得文件扩展名
        $temp_arr = explode(".", $sourceFile);
        $file_ext = array_pop($temp_arr);
        $file_ext = trim($file_ext);
        $file_ext = strtolower($file_ext);
        if(!file_exists($sourceFile)){
            echo "文件【".$imgfile."】不存在，请重试上传!";
            exit;
        }
        //检查扩展名
        if (in_array($file_ext, $ext_arr['image']) === false) {
            echo "要准备剪裁的文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr['image']) . "格式。";
            exit;
        }

        $resize_file_path = $php_path . str_replace('.'.$file_ext,'',$imgfile);
        //echo $resize_file_path;
        //剪切图片
        //包含剪切类
        require($php_path.'/plus/imagecorp.plus.php');
        try{
            $resize_img = new ImageCrop($sourceFile);
            //$resize_img->SetSrcFile($file_path);
            $resize_img->Crop(375,375,4);
            $resize_img->SaveImage(($resize_file_path .'!375!375.' . $file_ext));

            $resize_img->Crop(205,205,4);
            $resize_img->SaveImage(($resize_file_path .'!205!205.' . $file_ext));

            $resize_img->Crop(110,110,4);
            $resize_img->SaveImage(($resize_file_path .'!110!110.' . $file_ext));

            $resize_img->Crop(42,42,4);
            $resize_img->SaveImage(($resize_file_path .'!42!42.' . $file_ext));
            echo "剪裁成功！";
        }
        catch(customException $e){
            echo "剪裁失败！";
        }
        $resize_img = NULL;
    }
    /*
     * 取出商品的子分类
     */
    public function product_category_sub(){
        $id = $this->existsKeyInArray('id',$_REQUEST,'N');
        echo json_encode($this->mod_product_category_sub($id,'id,title'));
    }
	
}